package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysCommunityHouse;
import com.mars.module.admin.mapper.SysResidentMapper;
import com.mars.module.admin.service.ISysDictDataService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysFeePayRecordsRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysFeePayRecordsMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysFeePayRecords;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysFeePayRecordsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 物业费缴纳记录业务层处理
 *
 * @author mars
 * @date 2024-04-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysFeePayRecordsServiceImpl implements ISysFeePayRecordsService {

    private final SysFeePayRecordsMapper baseMapper;
    private final SysResidentMapper residentMapper;
    private final ISysDictDataService iSysDictDataService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysFeePayRecords add(SysFeePayRecordsRequest request) {
        SysFeePayRecords entity = SysFeePayRecords.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysFeePayRecordsRequest request) {
        SysFeePayRecords entity = SysFeePayRecords.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysFeePayRecords getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysFeePayRecords> pageList(SysFeePayRecordsRequest request) {
        Page<SysFeePayRecords> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysFeePayRecords> query = this.buildWrapper(request);
        IPage<SysFeePayRecords> pageRecord = baseMapper.selectPage(page, query);
        List<SysFeePayRecords> records = pageRecord.getRecords();
        List<SysFeePayRecords> recordsNew = new ArrayList<>();
        for (SysFeePayRecords payRecords : records) {
            payRecords.setUserName(residentMapper.selectById(payRecords.getUserId()).getName());
            payRecords.setTimeTypeName(iSysDictDataService.getDictDataById("time_type", payRecords.getTimeType().toString()));
            recordsNew.add(payRecords);
        }
        if (request.getUserName() != null) {
            recordsNew = recordsNew.stream().filter(e -> e.getUserName().equals(request.getUserName())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysFeePayRecords> list(SysFeePayRecordsRequest request) {
        LambdaQueryWrapper<SysFeePayRecords> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysFeePayRecords> buildWrapper(SysFeePayRecordsRequest param) {
        LambdaQueryWrapper<SysFeePayRecords> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getMoney())) {
            query.like(SysFeePayRecords::getMoney, param.getMoney());
        }
        if (param.getTimeType() != null) {
            query.like(SysFeePayRecords::getTimeType, param.getTimeType());
        }
        return query;
    }

}
